package www.sofn.com.goods;

import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import www.sofn.com.constant.Constants;
import www.sofn.com.goods.domain.query.GoodsDisplayQuery;
import www.sofn.com.goods.domain.vo.GoodsDisplayInfoVO;
import www.sofn.com.goods.domain.vo.GoodsDisplayListHostVO;

import java.util.List;
import java.util.Set;

@Service
public class GoodsDisplayService {

    @Resource
    private GoodsDisplayMapper mapper;
    public List<GoodsDisplayListHostVO> listHost(GoodsDisplayQuery query) {
        final List<GoodsDisplayListHostVO> listHostByQuery = mapper.getListHostByQuery(query);
        listHostByQuery.stream().forEach(v->{
            final String[] defaultImgTemp = v.getGoodsDefaultImg().split(",");
            if(defaultImgTemp.length<1){ v.setGoodsDefaultImg("");return; }
            if(!StrUtil.endWith(defaultImgTemp[0], Constants.Suffix_MP4))  {v.setGoodsDefaultImg(defaultImgTemp[0]);return ;}
            if(defaultImgTemp.length<2){ v.setGoodsDefaultImg("");return; }
            if( StrUtil.endWith(defaultImgTemp[0], Constants.Suffix_MP4)){
                v.setGoodsDefaultImg(defaultImgTemp[1]);
            }
        });
        return listHostByQuery;
    }

    @Cacheable(value = "goods", key = "'displayInfo:'+#root.args[0]")
    public GoodsDisplayInfoVO getGoodsDisplayInfoById(@NotBlank String id) {
        final GoodsDisplayInfoVO goodsDisplayInfoById =mapper.getGoodsDisplayInfoById(id);
      return   goodsDisplayInfoById;

    }

    public List<GoodsDisplayInfoVO> findGoodsInfo(Set<Long> goodsIds){
        return mapper.findGoodsInfo(goodsIds);
    }
}
